//
//  TFURLSessionManager.h
//  AFN_Test
//
//  Created by Guo Rui on 2017/9/11.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AFURLResponseSerialization.h"
#import "AFNetworkReachabilityManager.h"
#import "TFURLSessionManagerTaskDelegate.h"


typedef void(^TFURLSessionTaskDidCompleteBlock)(NSURLSession *session ,NSURLSessionTask *task, NSError *error);
typedef NSURLSessionResponseDisposition (^TFURLSessionDataTaskDidReciveResponseBlock)(NSURLSession *session, NSURLSessionDataTask *dataTask,  NSURLResponse *response);
//typedef void(^TFURLSessionDataTaskDidReciveDataBlock)(NSURLSession *session, NSURLSessionTask *task, NSData *data);

typedef void(^TFURLSessionDownloadTaskDidWriteDataBlock)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite);
//typedef void(^TFURLSessionDownloadTaskDidResumeBlock)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t fileOffset, int64_t expectedTotalBytes);

@interface TFURLSessionManager : NSObject

@property (strong, nonatomic, readonly) NSURLSession *session;
@property (strong, nonatomic) AFHTTPResponseSerializer *responseSerializer;
@property (strong, nonatomic) AFNetworkReachabilityManager *reachabilityManager;
@property (strong, nonatomic) NSOperationQueue *operationQueue;

@property (strong, nonatomic) dispatch_group_t completionGroup;
@property (strong, nonatomic) dispatch_queue_t completionQueue;

@property (copy, nonatomic) TFURLSessionTaskDidCompleteBlock taskDidComplete;
@property (copy, nonatomic) TFURLSessionDataTaskDidReciveResponseBlock didReciveResponse;
@property (copy, nonatomic) TFURLSessionDataTaskDidReciveDataBlock dataTaskDidReciveData;

@property (copy, nonatomic) TFURLSessionDownloadTaskDidWriteDataBlock downloadTaskDidWriteData;
@property (copy, nonatomic) TFURLSessionDownloadTaskDidResumeBlock downloadTaskDidResume;
@property (copy, nonatomic) TFURLSessionDownloadTaskDidFinishDownloadingBlock downloadTaskDidFinishDownloading;

@property (copy, nonatomic) TFURLSessionUploadTaskDidSendBodyDataBlock uploadTaskDidSendBodyData;

- (__kindof TFURLSessionManager *)initWithConfiguration:(NSURLSessionConfiguration *)configuration;

- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler;
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
                              uploadProgress:(nullable void(^)(NSProgress *uploadProgress))uploadProgress
                             downloadProgress:(nullable void(^)(NSProgress *downloadProgress))downloadProgress
                            completionHandler:(nullable void(^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler;


- (nonnull NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request
                                             progress:(nullable void (^)( NSProgress *downloadProgress))downloadProgressBlock
                                          destination:(nullable NSURL *(^)(NSURL *targetPath, NSURLResponse *response))destination
                                    completionHandler:(nullable void (^)(NSURLResponse *response, NSURL *fileURL, NSError *error))completionHandler;
- (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData *)resumeData
                                                progress:(nullable void (^)( NSProgress *downloadProgress))downloadProgressBlock
                                             destination:(nullable NSURL *(^)(NSURL *targetPath, NSURLResponse *response))destination
                                       completionHandler:(nullable void (^)(NSURLResponse *response, NSURL *fileURL, NSError *error))completionHandler;

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL uploadProgress:(void (^)(NSProgress *))uploadProgress completionHandler:(nullable void (^)(NSURLResponse *response, NSURL *fileURL, NSError *error))completionHandler;

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromData:(NSData *)bodyData uploadProgress:(void (^)(NSProgress *))uploadProgress completionHandler:(nullable void (^)(NSURLResponse *response, NSURL *fileURL, NSError *error))completionHandler;

@end
